package com.smart.community.feign.health.fallback;

import com.smart.community.feign.common.BaseFeignFallbackFactory;
import com.smart.community.feign.health.IHealthCheckFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 服务健康检查Feign降级处理工厂
 * 
 * 当服务健康检查接口调用失败时，提供降级处理
 * 确保监控系统能够正常工作
 * 
 * @author Wu.Liang
 * @version 2.0.0
 * @since 2025-08-28
 */
@Slf4j
@Component
public class HealthCheckFeignFallbackFactory extends BaseFeignFallbackFactory<IHealthCheckFeign> {

    @Override
    public IHealthCheckFeign create(Throwable cause) {
        if (isServiceUnavailable(cause)) {
            logServiceUnavailable(getServiceName(), cause);
        } else {
            log.warn("{}服务健康检查失败: {}", getServiceName(), cause.getMessage());
        }
        
        return new IHealthCheckFeign() {
            @Override
            public Boolean checkHealth() {
                logMethodFallback("checkHealth");
                return false; // 服务不可用时返回false
            }
            
            @Override
            public Map<String, Object> getServiceInfo() {
                logMethodFallback("getServiceInfo");
                Map<String, Object> info = new HashMap<>();
                info.put("status", "DOWN");
                info.put("message", "服务不可用");
                info.put("timestamp", System.currentTimeMillis());
                info.put("serviceName", getServiceName());
                return info;
            }
        };
    }
    
    @Override
    protected String getServiceName() {
        return "停车服务";
    }
}
